package com.sl.water.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.water.dao.PlanTransferDao;
import com.sl.water.dto.PlanTransferPersonDTO;
import com.sl.water.entity.PlanECunPerson;
import com.sl.water.entity.PlanTransfer;
import com.sl.water.entity.RehearsalScheme;
import com.sl.water.service.PlanECunPersonService;
import com.sl.water.service.PlanTransferService;
import com.sl.water.vo.BaseVO;
import com.sl.water.vo.PlanAzqVO;
import com.sl.water.vo.PlanTransferVO;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:避洪转移方案 服务接口实现
 * @author: duan
 * @time: 2025-06-26 14:37
 */
@Service
@RequiredArgsConstructor
public class PlanTransferServiceImpl extends ServiceImpl<PlanTransferDao, PlanTransfer> implements PlanTransferService {

    private final JdbcTemplate jdbcTemplate;
    private final PlanECunPersonService planECunPersonService;


    @Override
    public void removeByPlanId(Long planId) {
        LambdaQueryWrapper<PlanTransfer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlanTransfer::getPlanId, planId);
        this.remove(lambdaQueryWrapper);
    }

    @Override
    public void saveByRehearsalScheme(Long planId, RehearsalScheme rehearsalScheme) {
        List<PlanTransfer> planTransferList = new ArrayList<>();
        Integer status2 = rehearsalScheme.getStatus2();
        String kind = "bfh";
        Long rehearsalSchemeId = rehearsalScheme.getId();
        if (status2 == 2) {
            kind = "fh";
        }
        String dmSql = "SELECT DISTINCT b_r_dm.zh,b_e_dm.cun,b_e_dm.azq_id FROM b_r_dm LEFT JOIN b_e_dm ON b_r_dm.zh=b_e_dm.zh " +
                "WHERE b_r_dm.s_id='" + rehearsalSchemeId + "' and b_r_dm.kind='" + kind + "'";
        List<Map<String, Object>> dmList = jdbcTemplate.queryForList(dmSql);
        for (Map<String, Object> map : dmList) {
            PlanTransfer planTransfer = PlanTransfer.builder()
                    .name(String.valueOf(map.get("zh")))
                    .kind("河道漫溢")
                    .azlx("原地安置")
                    .planId(planId).build();

            if (ObjectUtil.isNotNull(map.get("cun"))) {
                Long azqId = null;
                if (ObjectUtil.isNotNull(map.get("azq_id"))) {
                    azqId = Long.valueOf(map.get("azq_id").toString());
                }
                createCommonInfo(map.get("cun").toString(), azqId,rehearsalScheme._getZylxKind(),planTransfer);
            }

            planTransferList.add(planTransfer);
        }
        String yldSql = "SELECT DISTINCT b_r_yld.name,b_e_yld.cun,b_e_yld.azq_id FROM b_r_yld LEFT JOIN b_e_yld ON b_r_yld.name=b_e_yld.name " +
                "WHERE b_r_yld.s_id='" + rehearsalSchemeId + "' and b_r_yld.kind='" + kind + "'";
        List<Map<String, Object>> yldList = jdbcTemplate.queryForList(yldSql);
        for (Map<String, Object> map : yldList) {
            PlanTransfer planTransfer = PlanTransfer.builder()
                    .name(String.valueOf(map.get("name")))
                    .kind("内涝")
                    .planId(planId).build();
            if (ObjectUtil.isNotNull(map.get("cun"))) {
                Long azqId = null;
                if (ObjectUtil.isNotNull(map.get("azq_id"))) {
                    azqId = Long.valueOf(map.get("azq_id").toString());
                }
                createCommonInfo(map.get("cun").toString(), azqId,rehearsalScheme._getZylxKind(),planTransfer);
            }
            planTransferList.add(planTransfer);
        }
        this.saveBatch(planTransferList);
    }

    private void createCommonInfo(String cun,Long azqId, int kind,PlanTransfer planTransfer) {
        //根据村查询安置人员
        PlanTransferPersonDTO planTransferPersonDTO = this.getPlanTransferPerson(cun);
        BeanUtil.copyProperties(planTransferPersonDTO, planTransfer);
        //根据村查询转移路线
        PlanAzqVO planAzqVO = this.getAzqVO(azqId,kind);
        if (ObjectUtil.isNotNull(planAzqVO)) {
            planTransfer.setZylx(planAzqVO.getZylx());
            planTransfer.setAzlx("就近安置");
            planTransfer.setAzq(planAzqVO.getAzq());
        }
    }



    private PlanAzqVO getAzqVO(Long azqId,int kind) {
        if(azqId==null)
            return null;
        PlanAzqVO result = new PlanAzqVO();
        String sql = "select distinct a.name as azq,b.name as zylx from plan_e_azq a join plan_e_zylx b on a.id=b.azq_id "
        +" where  a.id=? and b.kind=?";
        List<Map<String, Object>> yldList = jdbcTemplate.queryForList(sql,azqId,kind);
        if(yldList.size()==0)
            return null;
        result.setAzq(yldList.get(0).get("azq").toString());
        result.setZylx(yldList.get(0).get("zylx").toString());
        return result;
    }

    private PlanTransferPersonDTO getPlanTransferPerson(String cun) {
        LambdaQueryWrapper<PlanECunPerson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlanECunPerson::getCun, cun);
        List<PlanECunPerson> personList = planECunPersonService.list(lambdaQueryWrapper);
        return new PlanTransferPersonDTO(personList);
    }

    @Override
    public List<PlanTransferVO> queryByPlanId(Long id) {
        String sql = "SELECT pt.*, COALESCE(dm.cun, yld.cun) AS cun " +
                "FROM plan_transfer pt " +
                "LEFT JOIN b_e_dm dm ON pt.name = dm.zh AND pt.kind = '河道漫溢' " +
                "LEFT JOIN b_e_yld yld ON pt.name = yld.name AND pt.kind = '内涝' " +
                "WHERE pt.plan_id = ? " +
                "AND pt.kind IN ('河道漫溢','内涝')";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, id);
        return BaseVO.convertList(list, PlanTransferVO.class);
    }
}
